<template>
	<!-- 合作方管理 -->
	<div class="classification">
		<tableIndex
			:tableData="tableData"
			:tableHead="tableHead"
			:isIndex="true"
			:isSelection="true"
			:isRadio="false"
			:height="500"
			:page="page"
			:fields="fields"
			@selectChange="selectChange"
			@searchList="searchList"
			@page-change="pageChange"
		>
			<template #right-btn>
				<div style="float: right">
					<el-button
						size="small"
						class="button_edit"
						@click="typeHandler('add')"
						><svg-icon
							icon-class="add-icon-1"
							class="icon-export m-r5"
						></svg-icon
						>新增</el-button
					>
					<el-button
						size="small"
						class="button_delete"
						@click="deleteHandler()"
						>批量删除</el-button
					>
				</div>
			</template>

			<template #status="{ row }">
				<el-switch v-model="row.status" @change="statusChangeHandel(row)" />
			</template>

			<template #operate>
				<el-table-column label="操作" width="200" fixed="right">
					<template #default="{ row }">
						<el-button
							size="small"
							class="button_edit"
							@click="typeHandler('edit', row)"
							type="primary"
							>编辑</el-button
						>
						<el-button
							size="small"
							class="button_edit"
							@click="deleteHandler(row)"
							type="primary"
							><svg-icon icon-class="delete-icon"
						/></el-button>
					</template>
				</el-table-column>
			</template>
		</tableIndex>

		<!-- 编辑 新建 分类弹窗 -->
		<public-dialog
			:title="title"
			:width="'30%'"
			v-model:visible="visible"
			@onConfirm="onConfirm"
		>
			<el-form ref="formRef" :model="cooperationForm" label-width="120">
				<el-form-item
					label="合作方名称"
					prop="partnerName"
					:rules="{
						required: true,
						message: '请输入',
						trigger: 'blur',
					}"
				>
					<el-input v-model="cooperationForm.partnerName"> </el-input>
				</el-form-item>
				<!-- <el-form-item label="描述" prop="remark">
					<el-input v-model="cooperationForm.remark" type="textarea">
					</el-input>
				</el-form-item> -->
			</el-form>
		</public-dialog>
	</div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from "vue";
import TableIndex from "@/components/table/index.vue";
import { useRoute, useRouter } from "vue-router";
import { ElRow, ElCol, ElMessageBox, ElMessage, ElForm } from "element-plus";
import { cooperationApi } from "@/api/investment.js";
import PublicDialog from "@/components/PublicDialog.vue";
const route = useRoute();
const router = useRouter();

const state = reactive({});
const page = reactive({
	page: 1,
	pageSize: 10,
	total: 0,
});
const cooperationForm = ref({});
const visible = ref(false);
const title = ref("新增");
const fields = ref([
	{
		key: "keyword",
		label: "搜索内容",
		type: "input",
		placeholder: "合作方名称关键字搜索",
	},
]);
let tableData = ref([]);
let tableHead = ref([
	{
		prop: "partnerName",
    label: "合作方名称",
    minWidth: 100
	},
	{
		prop: "createTime",
    label: "创建时间",
    minWidth: 150
	},
	{
		prop: "createUserName",
    label: "创建人",
    minWidth: 100
	},
	// {
	// 	prop: "remark",
	// 	label: "描述",
	// },
]);
const selectList = ref([]);
const selectChange = (e) => {
	selectList.value = e;
};

//分页
const pageChange = (e) => {
	console.log(e);
	page.page = e.page;
	page.pageSize = e.limit;

	getPage(page);
};
//搜索接口请求
const searchList = (e) => {
	getPage(e);
};
const getPage = (e) => {
	cooperationApi
		.getPage({
			pageNum: page.page,
			pageSize: page.pageSize,
			param: e,
		})
		.then((res) => {
			if (res.code == 200) {
				tableData.value = res.data.content;
				page.total = res.data.totalSize;
			}
		});
};

//删除
const deleteHandler = (row) => {
	let ids = [];
	if (row) {
		ids = [row.partnerId];
	} else {
		if (selectList.value.length == 0) {
			return ElMessage.warning("请选择要删除的数据");
		}
		ids = selectList.value.map((item) => item.partnerId);
	}
	ElMessageBox.confirm("确认删除吗?", {
    confirmButtonText: "确认",
    confirmButtonClass: "button_view",
    cancelButtonText: "取消",
    cancelButtonClass: "button_cancel2",
		type: "warning",
	})
		.then(() => {
			cooperationApi.partnerDelete({ ids }).then((res) => {
				if (res.code == 200) {
					ElMessage.success("删除成功");
					getPage();
				}
			});
		})
		.catch(() => {});
};

const typeHandler = (type, row) => {
	if (type == "add") {
		title.value = "新增";
	} else {
		title.value = "编辑";
		cooperationForm.value = {
			...row,
		};
	}
	visible.value = true;
};
const formRef = ref(null);
const onConfirm = () => {
	formRef.value.validate((valid) => {
		if (valid) {
			cooperationApi.partnerAddAndUp(cooperationForm.value).then((res) => {
				if (res.code == 200) {
					ElMessage.success("操作成功");
					visible.value = false;
					getPage();
				}
			});
		}
	});
};

watch(
	() => visible.value,
	(val) => {
		if (!val) {
			cooperationForm.value = {};
		}
	}
);

onMounted(() => {
	getPage();
});
</script>
<style lang="scss" scoped>
</style>
